package org.jeecg.modules.system.controller;

import cn.hutool.core.util.URLUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.system.entity.SysCompanyInfo;
import org.jeecg.modules.system.service.ISysCompanyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @Author scott
 * @since 2018-12-20
 */
@Slf4j
@RestController
@RequestMapping("/sys/company")
public class CompanyInfoController {

    @Autowired
    private ISysCompanyInfoService companyInfoService;


    @GetMapping("/list")
    public Result list(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                       @RequestParam(name = "pageSize", defaultValue = "100") Integer pageSize,
                       @RequestParam(name = "type", defaultValue = "1") Integer type
                       ) {
        Page<SysCompanyInfo> page = new Page<SysCompanyInfo>(pageNo, pageSize);
        Page<SysCompanyInfo> page1 = companyInfoService.page(page,Wrappers.<SysCompanyInfo>lambdaQuery()
                .eq(SysCompanyInfo::getType,type)
        );
        return Result.OK(page1);
    }
    @GetMapping("/list/all")
    public Result listAll() {
        List<SysCompanyInfo> list = companyInfoService.list();
        return Result.OK(list);
    }


    @GetMapping("/getCompanyCode/{companyName}")
    public Result getCompanyCode(@PathVariable String companyName) {
        SysCompanyInfo one = null;
        try {
            one = companyInfoService.getOne(Wrappers.<SysCompanyInfo>lambdaQuery()
                    .eq(SysCompanyInfo::getCompanyName, companyName)
                    .eq(SysCompanyInfo::getType, 1)
            );
            return Result.OK(one.getCompanyCode());
        } catch (Exception e) {
            return Result.error("公司信息不存在或异常");
        }
    }


    @PostMapping("/add")
    public Result add(@RequestBody SysCompanyInfo sysCompanyInfo) {
        if (StringUtils.isAnyBlank(sysCompanyInfo.getCompanyCode(), sysCompanyInfo.getCompanyName())) {
            return Result.error("请检查公司名称或公司代码是否填写。");
        }

        long count = companyInfoService.count(Wrappers.<SysCompanyInfo>lambdaQuery()
                .eq(SysCompanyInfo::getCompanyCode, sysCompanyInfo.getCompanyCode())
                .or()
                .eq(SysCompanyInfo::getCompanyName, sysCompanyInfo.getCompanyName())
        );
        if (count > 0) {
            return Result.error("公司名称或代码已存在");
        }
        companyInfoService.save(sysCompanyInfo);

        return Result.OK();
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody SysCompanyInfo sysCompanyInfo) {

        if (StringUtils.isBlank(sysCompanyInfo.getId())) {
            return Result.error("公司信息不存在无法进行编辑[id is null]");
        }

        if (StringUtils.isAnyBlank(sysCompanyInfo.getCompanyCode(), sysCompanyInfo.getCompanyName())) {
            return Result.error("请检查公司名称或公司代码是否填写。");
        }

        long count = companyInfoService.count(Wrappers.<SysCompanyInfo>lambdaQuery()
                .ne(SysCompanyInfo::getId, sysCompanyInfo.getId())
                .and(w -> w.eq(SysCompanyInfo::getCompanyCode, sysCompanyInfo.getCompanyCode())
                        .or()
                        .eq(SysCompanyInfo::getCompanyName, sysCompanyInfo.getCompanyName()))
        );
        if (count > 0) {
            return Result.error("公司名称或代码已存在");
        }
        companyInfoService.updateById(sysCompanyInfo);

        return Result.OK();
    }


    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result<SysCompanyInfo> delete(@RequestParam(name = "id", required = true) String id) {

        Result<SysCompanyInfo> result = new Result<SysCompanyInfo>();
        SysCompanyInfo sysCompanyInfo = companyInfoService.getById(id);
        if (sysCompanyInfo == null) {
            result.error500("未找到对应实体");
        } else {
            companyInfoService.removeById(id);
            result.success("删除成功!");
        }
        return result;
    }


    /**
     * 批量删除 根据前端请求的多个ID,对数据库执行删除相关部门数据的操作
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public Result<SysCompanyInfo> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {

        Result<SysCompanyInfo> result = new Result<SysCompanyInfo>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            companyInfoService.removeBatchByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }

}
